//
//  SetupViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/10.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "SetupViewController.h"

@interface SetupViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
    
    UIImagePickerController *_imagePickerController;
}

@property (nonatomic, weak)UIButton * backButton;
@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, weak)UIButton * iconButton;
@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, strong)UITextField * nameText;
@property (nonatomic, strong)UITextField * ganderText;
@property (nonatomic, strong)UITextField * ageText;
@property (nonatomic, strong)UITextField * xinzuoText;
@property (nonatomic, strong)UITextField * adressText;
@property (nonatomic, strong)UITextField * telText;
@property (nonatomic, strong)UITextField * passwordText;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, strong)UIView * fourLineView;
@property (nonatomic, strong)UIView * fiveLineView;
@property (nonatomic, strong)UIView * sixLineView;
@property (nonatomic, strong)UIView * sevenLineView;
@property (nonatomic, weak)UIButton * setupButton;
@property (nonatomic, strong)UIAlertController * alertVC;


@end

@implementation SetupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE_COLOR;
    
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    _imagePickerController.allowsEditing = YES;

    [self setupUI];
    [self setupAlertVC];
}



- (void)setupUI{
    
    UIImageView * backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    
    backImageView.image = ImageNamed(@"bg");
    backImageView.userInteractionEnabled = YES;
    
    self.backImageView = backImageView;
    
    [self.view addSubview:self.backImageView];
    
    
    UIButton * backButton = [[UIButton alloc] init];

    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    self.backButton = backButton;
    
    [self.view addSubview:self.backButton];
    
    
    UIButton * iconButton = [UIButton new];
    
    [iconButton setBackgroundColor:[UIColor lightGrayColor]];
    [iconButton setTitle:@"  设置头像  " forState:UIControlStateNormal];
    [iconButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [iconButton addTarget:self action:@selector(clilkAddIconImage) forControlEvents:UIControlEventTouchUpInside];
    iconButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    //  把图片设置成圆形
    iconButton.layer.cornerRadius = 40;//裁成圆角
    iconButton.layer.masksToBounds = YES;//隐藏裁剪掉的部分
    
    self.iconButton = iconButton;
    
    [self.view addSubview:self.iconButton];
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = ColorWithRGB(0xDA4136);
    titleLabel.text = @"基本资料";
    titleLabel.font = [UIFont systemFontOfSize:18 * SCREENH6];
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    self.nameText = [self setupTextFiled:@"昵称"];
    [self.view addSubview:self.nameText];
    
    self.oneLineView = [self setupLineView];
    [self.view addSubview:self.oneLineView];
    
    self.ganderText = [self setupTextFiled:@"性别"];
    [self.view addSubview:self.ganderText];
    
    self.twoLineView = [self setupLineView];
    [self.view addSubview:self.twoLineView];
    
    self.ageText = [self setupTextFiled:@"年龄"];
    [self.view addSubview:self.ageText];
    
    self.threeLineView = [self setupLineView];
    [self.view addSubview:self.threeLineView];
    
    self.xinzuoText = [self setupTextFiled:@"星座"];
    [self.view addSubview:self.xinzuoText];
    
    self.fourLineView = [self setupLineView];
    [self.view addSubview:self.fourLineView];
    
    self.adressText = [self setupTextFiled:@"地址"];
    [self.view addSubview:self.adressText];
    
    self.fiveLineView = [self setupLineView];
    [self.view addSubview:self.fiveLineView];
    
    self.telText = [self setupTextFiled:@"手机号"];
    [self.view addSubview:self.telText];
    
    self.sixLineView = [self setupLineView];
    [self.view addSubview:self.sixLineView];
    
    self.passwordText = [self setupTextFiled:@"密码"];
    [self.view addSubview:self.passwordText];
    
    self.sevenLineView = [self setupLineView];
    [self.view addSubview:self.sevenLineView];
    
    
    UIButton * setupButton = [[UIButton alloc] init];
    
    [setupButton setTitle:@"立即注册" forState:UIControlStateNormal];
    setupButton.titleLabel.font = [UIFont boldSystemFontOfSize:17 * SCREENH6];
    [setupButton setBackgroundColor:ColorWithRGB(0xDA4136)];
    
    [setupButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [setupButton addTarget:self action:@selector(didSetupButton) forControlEvents:UIControlEventTouchUpInside];
    //  把图片设置成圆形
    setupButton.layer.cornerRadius = 20;//裁成圆角
    setupButton.layer.masksToBounds = YES;//隐藏裁剪掉的部分
    
    self.setupButton = setupButton;
    
    [self.view addSubview:self.setupButton];
}


-(void)viewDidLayoutSubviews{
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backImageView).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.backImageView).offset(25 * SCREENH6);
        make.width.mas_equalTo(12 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];
    
    [self.iconButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(40 * SCREENH6);
        make.top.mas_equalTo(self.backButton.mas_bottom).offset(25 * SCREENH6);
        make.width.height.mas_equalTo(80 * SCREENH6);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconButton);
        make.top.mas_equalTo(self.iconButton.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.nameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.nameText);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.nameText.mas_bottom);
    }];
    
    [self.ganderText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.nameText);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(8 * SCREENH6);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.ganderText.mas_bottom);
    }];
    
    [self.ageText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.ganderText);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(8 * SCREENH6);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.ageText.mas_bottom);
    }];
    
    [self.xinzuoText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.ageText);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(8 * SCREENH6);
    }];
    
    [self.fourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.threeLineView);
        make.top.mas_equalTo(self.xinzuoText.mas_bottom);
    }];
    
    [self.adressText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.xinzuoText);
        make.top.mas_equalTo(self.fourLineView.mas_bottom).offset(8 * SCREENH6);
    }];
    
    [self.fiveLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.fourLineView);
        make.top.mas_equalTo(self.adressText.mas_bottom);
    }];
    
    [self.telText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.adressText);
        make.top.mas_equalTo(self.fiveLineView.mas_bottom).offset(8 * SCREENH6);
    }];
    
    [self.sixLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.fiveLineView);
        make.top.mas_equalTo(self.telText.mas_bottom);
    }];
    
    [self.passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.telText);
        make.top.mas_equalTo(self.sixLineView.mas_bottom).offset(8 * SCREENH6);
    }];
    
    [self.sevenLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.sixLineView);
        make.top.mas_equalTo(self.passwordText.mas_bottom);
    }];
    
    [self.setupButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.passwordText);
        make.top.mas_equalTo(self.sevenLineView.mas_bottom).offset(40 * SCREENH6);
    }];
}


- (UITextField *)setupTextFiled:(NSString *)string{
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = [UIColor clearColor];
    
//    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
//    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
//    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    //    passwordText.delegate = self;
    //设置光标颜色
    textFiled.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
//    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:string attributes:attrs];
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.textColor = [UIColor whiteColor];
//    textFiled.textAlignment = NSTextAlignmentCenter;
    textFiled.keyboardAppearance = UIKeyboardAppearanceDark;
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60 * SCREENH6, 20 * SCREENH6)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 20)];//WithImage:[UIImage imageNamed:@"手机号"]];
    label.text = string;
    label.font = [UIFont systemFontOfSize:15 * SCREENH6];
    label.textColor = WHITE_COLOR;
    [leftView addSubview:label];
    textFiled.leftView = leftView;
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    
    return textFiled;
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = WHITE_COLOR;
    lineView.alpha = 0.6;
    
    return lineView;
}


- (void)clilkAddIconImage{
    
    [self presentViewController:self.alertVC animated:YES completion:nil]; 
}



- (void)setupAlertVC{
    
    self.alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"选择照片方式" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * camerAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self->_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:self->_imagePickerController animated:YES completion:nil];
    }];
    
    UIAlertAction * albumsAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self->_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        [self presentViewController:self->_imagePickerController animated:YES completion:nil];
    }];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [self.alertVC addAction:camerAction];
    [self.alertVC addAction:albumsAction];
    [self.alertVC addAction:cancelAction];
    
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo

{
    
    [self.iconButton setImage:image forState:UIControlStateNormal];
    
    //    NSData *frontImageData = UIImageJPEGRepresentation(image,0.1);
    
    //    [[ZSNetworkTool sharedTool] commitChangeMyIconWithfiles:frontImageData resultWithBlock:^(BOOL success, id responseData) {
    //
    //
    //        //        NSLog(@"responseData==%@",responseData);
    //    }];
    //
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//进入拍摄页面点击取消按钮

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)didSetupButton{
    
    if (self.telText.text.length > 0 && self.passwordText.text.length > 0) {
        
        NSDictionary * dict = @{@"username":self.nameText.text,@"sex":self.ganderText.text,@"mobile":self.telText.text,@"password":self.passwordText.text,@"age":self.ageText.text,@"address":self.adressText.text,@"constellation":self.xinzuoText.text};
        
        [[NetworkTool sharedTools] POST:@"/index/Member/register" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSString * codeString = [NSString stringWithFormat:@"%@",responseObject[@"status"]];
            
            if ([codeString isEqualToString:@"200"]) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"恭喜你注册成功！赶快去登录吧！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请完善注册信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    
    
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
